package com.sxt.bbs.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 管理数据库连接
 * @author lovemanai
 */
public class DB {
	/**
	 * 获得一个与数据库的连接
	 * @return Connetion conn
	 */
	public static Connection getConn() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root", "root");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 产生一个Statement对象
	 * @param conn 数据库连接
	 * @return Statement stat
	 */
	public static Statement createStat(Connection conn) {
		Statement stat = null;
		try {
			stat = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stat;
	}
	
	/**
	 * 重载createPStat方法
	 * 产生一个PreparedStatement对象
	 * @param conn 数据库连接
	 * @param sql 待执行sql语句
	 * @return PreparedStatement pStat
	 */
	public static PreparedStatement createPStat(Connection conn, String sql) {
		PreparedStatement pStat = null;
		try {
			pStat = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pStat;
	}
	/**
	 * 重载creatPSta方法
	 * @param conn
	 * @param sql
	 * @param autoGeneratedKeys
	 * @return
	 */
	public static PreparedStatement createPStat(Connection conn, String sql, int autoGeneratedKeys) {
		PreparedStatement pStat = null;
		try {
			pStat = conn.prepareStatement(sql, autoGeneratedKeys);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pStat;
	}
	
	
	/**
	 * 执行sql语句返回一个Result对象用于对数据集各个字段进行遍历
	 * @param stat
	 * @param sql
	 * @return
	 */
	public static ResultSet excuteQuerey(Statement stat, String sql) {
		ResultSet rs = null;
		try {
			
			rs = stat.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * 
	 * @param conn
	 * @param sql
	 * @return
	 */
	public static int excuteUpdate(Connection conn, String sql) {
		int ret = 0;
		Statement stat = null;
		try {
			stat = conn.createStatement();
			stat.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(stat);
		}
		return ret;
	}
	
	/**
	 * 重载close()方法用于关闭Connection
	 * @param conn
	 */
	public static void close(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn = null;
		}
	}
	
	/**
	 * 重载close()方法用于关闭Statement
	 * @param stat
	 */
	public static void close(Statement stat) {
		if(stat != null) {
			try {
				stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stat = null;
		}
	}
	
	/**
	 * 重载close()方法用于关闭ResultSet
	 * @param rs
	 */
	public static void close(ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs = null;
		}
	}
	
}
